#!/bin/sh
#
#  This file is part of the sdssidl IDL routines. 
#  Configure script for SDSSIDL C/C++ libraries
#
#  Copyright (C) 2005  Erin Sheldon, NYU.  erin.sheldon at gmail.com
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA


#
# Will create the src/codedirs file and create system dependent variables
# for the make files
#

if [ ! ${IDL_DIR:+1} ]; then
    echo "Environment variable IDL_DIR is undefined.  IDL_DIR required to include export.h"
    exit 45
fi


# Create the src/codedirs file.  This will contain the basic code 
# directories. 
#
# NOTE: sdsspixIDL is special because it requires the GSL libraries

SRC_CODE_DIRS="src/codedirs"

# IDLStruct is now header-only
#CODE_DIRS="IDLStruct atlas fileio gauleg htmIndexIDL sphpoly_masks total_int"
CODE_DIRS="IDLStruct atlas fileio gauleg htm sphpoly_masks total_int"

# First we must check if the gnu-scientific library is here
# if so, add sdsspixIDL to the CODE_DIRS

incdirs="/usr/local/include/gsl /usr/include/gsl /sw/include/gsl"
gslTestFile="gsl_fft.h"
gslExists="NO"
for incDir in $incdirs; do
    if [ -e ${incDir}/${gslTestFile} ]; then
	echo
	echo Gnu Scientific Libraries found in $incDir
	echo sdsspixIDL will be built
	sleep 1
	gslExists="YES"
	CODE_DIRS="${CODE_DIRS} sdsspixIDL"
	break
    fi
done

if [ "$gslExists" == "NO" ]; then
    echo Gnu Scientific Libraries not found in $incdirs. Check paths.
    echo "sdsspixIDL will NOT be built (This is OK)"
    sleep 5
fi

# Check if the postgres libraries are on the
# machine. If so, compile postgres module pgsql
# Note, a common mistake is to install postgres in /usr/local/pgsql
# but not update the /etc/ld.so.conf and run ldconfig
incdirs="/usr/local/pgsql/include /usr/local/include /usr/include /sw/include/postgresql /sw/opt/postgresql-8.4/include  /sw/opt/postgresql-8.3/include /sw/opt/postgresql-8.2/include /sw/opt/postgresql-8.1/include /sw/opt/postgresql-8.0/include"
pgTestFile="libpq-fe.h"
pgExists="NO"
for incDir in $incdirs; do
    if [ -e ${incDir}/${pgTestFile} ]; then
	echo
	echo Postgres headers found in $incDir
	echo pgsql will be built
	sleep 1
	pgExists="YES"
	CODE_DIRS="${CODE_DIRS} pgsql"
	break
    fi
done

if [ "$pgExists" == "NO" ]; then
    echo Postgres headers not found in: $incdirs.  Check paths.
    echo "pgsql will NOT be built (this is OK)"
    sleep 5
fi



# create the codedirs file
echo
echo Creating $SRC_CODE_DIRS

if [ -e $SRC_CODE_DIRS ]; then
    cp -f $SRC_CODE_DIRS ${SRC_CODE_DIRS}.bak
fi

echo "#" > $SRC_CODE_DIRS
echo "# Directories of routines to be built" >> $SRC_CODE_DIRS
echo "#" >> $SRC_CODE_DIRS
echo >> $SRC_CODE_DIRS
echo "CODE_DIRS = ${CODE_DIRS}" >> $SRC_CODE_DIRS
echo >> $SRC_CODE_DIRS

# Create the make.conf file for this OS
echo "Making src/make.conf for this OS"
cd src
./configure

echo
echo Ready. You can type \"make\" now

